import json


class Score:

    def __init__(self, valipath='vali.json', querypath='query.json'):
        self.querypath = querypath
        self._flag = 0
        self._count = {}
        with open(valipath, 'rb') as f:
            self.__vali_dict = json.load(f)

        with open(querypath, 'rb') as f1:
            self.__query_dict = json.load(f1)

    def _validate(self):

        while True:
            try:
                status = input('>>teacher or student or close').strip()
                user_dict = self.__vali_dict[status]
                setattr(self, 'username', input('>>请输入用户名').strip())

                if self.username in self._count and self._count[self.username] > 2:
                    print('账户锁定，请登录其他账户')
                    continue
                setattr(self, 'password', input('>>请输入密码').strip())

                if self.username in user_dict and self.password == user_dict[self.username]:
                    if status == 'teacher':
                        self._flag = 1
                        self._input()
                        continue
                    else:
                        self._flag = 2
                        self._query()
                        continue
                else:
                    self._count[self.username] = self._count.get(self.username, 0) + 1
                    print('用户名密码错误')

            except KeyError:
                if status == 'close':
                    break
                print('请输入有效值')

    def _input(self):
        if self._flag != 1:
            print('无权限操作')
            return

        def check(score):
            if score < 0 or score > 100:
                raise Exception('分数有误')
            else:
                return score

        if input('>>是否输入成绩y/n').strip() == 'y':
            while True:
                name = input('>>请输入学生姓名').strip()
                if name in self.__vali_dict['student']:
                    Chinese = int(input('>>请输入语文成绩').strip())
                    Math = int(input('>>请输入数学成绩').strip())
                    English = int(input('>>请输入英语成绩').strip())

                    try:
                        self.__query_dict.update({name: {'语文': check(Chinese), '数学': check(Math), '英语': check(English)}})

                        if input('>>是否继续添加成绩y/n') == 'n':
                            with open(self.querypath, 'w') as f:
                                print(self.__query_dict, f, type(f))
                                json.dump(self.__query_dict, f)
                            break

                    except Exception as e:
                        print(e)
                        continue

                else:
                    print('学生名不存在')
                    continue

        elif input('>>是否查询成绩y/n').strip() == 'y':
            self._query()

    def _query(self):
        while True:
            name = input('>>请输入学生姓名').strip()
            if name in self.__query_dict:
                print(self.__query_dict[name])

                if input('>>是否继续查询y/n') == 'n':
                    break

            else:
                print('未查到结果')

    def run(self):
        self._validate()


a = Score()
a.run()
